'''
Created on Aug 29, 2011

@author: bstar
'''
import os
import logging
import traceback

from twisted.internet import reactor 

from dopplershift.core.utils.logger import DSLogging
from dopplershift.core.events import DSEventManager
from dopplershift.tx.clients.ftp import events as ftpEvents
from dopplershift.tx.clients.ftp.factory import DSFtpFactory

logger = logging.getLogger(__name__)




class Load:
    def __init__(self):
        DSLogging('DEBUG')
        kwargs = {
          'username': 'user',
          'password': '12345',
          'port': 10000,
          'host': '127.0.0.1',
          'path': 'upload'
          }
        
        self._event_manager = DSEventManager(
                                             []                                             
                                             )
        self.ffs = DSFtpFactory(
                                self._event_manager,
                                **kwargs
                                )
        self._event_manager.sign(ftpEvents.EVENT_FTP_CLIENT_FILE_UPLOADED, self.file_uploaded)
        self._event_manager.sign(ftpEvents.EVENT_FTP_CLIENT_UPLOAD_QUEUE_END, self.queue_finish)
        
        flist = [
                 '/home/bstar/Downloads/gpluswin.gif',
                 '/home/bstar/Downloads/eclipse-SDK-3.6.2-linux-gtk-x86_64.tar.gz', 
                 #'/home/bstar/Downloads/eclipse-SDK-3.6.2-linux-gtk.tar.gz'
                 ]
        self.ffs.queue(flist)
        self.ffs.connect()
    def file_uploaded(self, filename):
        logger.info("File %s uploaded"%filename)
    def queue_finish(self, ignored):
        try:
            self.ffs.stop()
        except:
            traceback.print_exc()
        logger.info("Queue Finished, reactor stopped two seconds later")
        reactor.callLater(2, reactor.stop)
    def run(self):
        reactor.run()
l = Load()
l.run()